package com.jsmtr.rms.utils.db;

import java.util.ArrayList;

/**
 * 列集合
 *
 * @author 陈宇翔
 */
public class DbColumnCollection extends ArrayList<DbColumn> {
    private static final long serialVersionUID = -8940196742313994740L;
    private DbRowCollection rowObjs = new DbRowCollection();


    public DbColumnCollection() {
    }

    /**
     * 列可见行
     *
     * @param Columnobjs
     */
    void set(DbRowCollection rowobjs) {
        rowObjs = rowobjs;
        for (int i = 0; i < rowobjs.length(); i++) {
            super.add(new DbColumn(rowobjs, i, rowobjs.getColName(i)));
        }
    }


    /**
     * 在指定列索引位置插入一列数据
     */
    public void add(int index, DbColumn dbcolumnobj) {
        DbColumn dbcolumn = (DbColumn) dbcolumnobj.clone();
        try {
            if (rowObjs.size() != dbcolumn.size()) {
                System.out.println("DbColumnCollection.addDbColumn(int,DbColumn).err:列对象行数" + dbcolumn.size() + "不等于虚拟表行数" + rowObjs.size());
            }
            if (index > rowObjs.length()) {
                index = rowObjs.length();
            }
            String colName = dbcolumn.getColumnName();
            rowObjs.colNameList.add(index, colName);
            for (int i = 0; i < rowObjs.size(); i++) {
                DbRow dbrowObj = rowObjs.get(i);
                dbrowObj.addColName(index, colName);
                dbrowObj.put(colName, dbcolumn.get(i));
            }
        } catch (Exception e) {
            System.out.println("DbColumnCollection.addDbColumn(int,DbColumn).err:" + e);
        }
    }


    /**
     * 在最末插入一列数据
     */
    public boolean add(DbColumn dbcolumnobj) {
        DbColumn dbcolumn = (DbColumn) dbcolumnobj.clone();
        try {

            if (rowObjs.size() != dbcolumn.size()) {
                System.out.println("DbColumnCollection.add(DbColumn).err:列对象行数" + dbcolumn.size() + "不等于虚拟表行数" + rowObjs.size());
                return false;
            }
            String colName = dbcolumn.getColumnName();

            for (int i = 0; i < rowObjs.size(); i++) {
                DbRow dbrowObj = rowObjs.get(i);//取出每一行
                dbrowObj.addColName(colName);//对每一行添加列名
                dbrowObj.put(colName, dbcolumn.get(i));//在每一行末尾添加数据
            }
            rowObjs.colNameList.add(dbcolumn.getColumnName());
            return true;
        } catch (Exception e) {
            System.out.println("DbColumnCollection.add(DbColumn).err:" + e);
            return false;
        }
    }


    /**
     * 根据索引得到列对象
     */
    public DbColumn get(int index) {
        this.clear();
        set(this.rowObjs);
        if (index <= this.size()) {
            return (DbColumn) super.get(index);
        } else {
            System.out.println("DbColumnCollection.getDbColumn(int).err:指定索引：" + index + "超出列最大索引位置:" + this.size());
        }
        return null;
    }

    /**
     * 根据名称得到列对象
     */
    public DbColumn get(String ColumnName) {
        this.clear();
        set(this.rowObjs);
        for (int i = 0; i < this.size(); i++) {
            DbColumn ColumnObj = (DbColumn) super.get(i);
            if (ColumnObj.getColumnName().equals(ColumnName)) {
                return ColumnObj;
            }
        }
        return null;
    }

    /**
     * 移除指定列索引的一列
     */
    public boolean removeDbColumn(int index) {
        if (index <= this.size()) {
            try {

                for (int i = 0; i < rowObjs.size(); i++) {
                    DbRow sss = rowObjs.get(i);
                    sss.remove(sss.getColName(index));//map
                    sss.getColNameList().remove(index);//list
                }
                rowObjs.colNameList.remove(index);//collection list
                this.remove(index);
                return true;
            } catch (Exception e) {
                System.out.println("DbColumnCollection.remove.err:" + e);
            }
        } else {
            System.out.println("DbColumnCollection.removeDbColumn(int).err:指定索引：" + index + "超出列最大索引位置:" + this.size());
        }
        return false;
    }

    /**
     * 移除指定列名的一列
     */
    public boolean removeDbColumn(String ColumnName) {
        try {
            DbRow sss = rowObjs.get(0);
            int index = sss.getColIndex(ColumnName);
            if (index == -1) {
                System.out.println("DbColumnCollection.removeDbColumn(ColumnName).err:当前列名不存在");
                return false;
            }
            for (int i = 0; i < rowObjs.size(); i++) {
                sss = rowObjs.get(i);
                sss.remove(ColumnName);
                sss.getColNameList().remove(index);

            }
            rowObjs.colNameList.remove(index);
            this.remove(index);
            return true;
        } catch (Exception e) {
            //System.out.println("DbColumnCollection.removeDbColumn(ColumnName).err:"+e);
        }
        return false;
    }


    /**
     * 修改指定列集合的列
     */
    public DbColumn set(int index, DbColumn dbcolumn) {
        String colName = dbcolumn.getColumnName();
        for (int i = 0; i < rowObjs.size(); i++) {
            DbRow dbrowObj = rowObjs.get(i);//取出每一行
            dbrowObj.remove(dbrowObj.getColName(index));//移除原来的数据
            dbrowObj.setColName(index, colName);//修改指定索引的列名
            dbrowObj.put(colName, dbcolumn.get(i));//添加数据
        }
        rowObjs.colNameList.set(index, dbcolumn.getColumnName());
        return dbcolumn;
    }


}
